#include "pinctrl.h"
#include "uart.h"
#include "osal_debug.h"
#include "soc_osal.h"
#include "app_init.h"
#include "string.h"
#include "cmsis_os2.h"
#include "uart_rx_tx.h"
void uart2_read_handler(const void *buffer, uint16_t length, bool error);
void uart1_read_handler(const void *buffer, uint16_t length, bool error);


#define DELAY_TIME_MS 1
#define UART_RECV_SIZE 10
#define UART_RX_MAX 512
uint8_t uart_recv[UART_RECV_SIZE] = {0};
uint8_t g_uart_rx_buffer[UART_RX_MAX];




void uart_gpio_init(void)
{
    //uapi_pin_set_mode(GPIO_16, PIN_MODE_1);
   // uapi_pin_set_mode(GPIO_15, PIN_MODE_1);
  //  uapi_pin_set_mode(GPIO_16, PIN_MODE_1);
    //uapi_pin_set_mode(GPIO_15, PIN_MODE_1);
    uapi_pin_set_mode(GPIO_07, PIN_MODE_2);
    uapi_pin_set_mode(GPIO_08, PIN_MODE_2);

}

void uart_init_config(void)
{
    uart_buffer_config_t uart_buffer_config;
    uart_attr_t attr = {
        .baud_rate = 115200, .data_bits = UART_DATA_BIT_8, .stop_bits = UART_STOP_BIT_1, .parity = UART_PARITY_NONE};
    uart_buffer_config.rx_buffer_size = UART_RX_MAX;
    uart_buffer_config.rx_buffer = g_uart_rx_buffer;
    uart_pin_config_t pin_config0 = {.tx_pin = S_MGPIO0, .rx_pin = S_MGPIO1, .cts_pin = PIN_NONE, .rts_pin = PIN_NONE};
    uart_pin_config_t pin_config2 = {.tx_pin = GPIO_08,  .rx_pin = GPIO_07, .cts_pin = PIN_NONE, .rts_pin = PIN_NONE};
    //uart_pin_config_t pin_config1 = {.tx_pin = GPIO_15,  .rx_pin = GPIO_16, .cts_pin = PIN_NONE, .rts_pin = PIN_NONE};
    uapi_uart_deinit(UART_BUS_0);
    //uapi_uart_deinit(UART_BUS_1);
    uapi_uart_deinit(UART_BUS_2);
    int res0 = uapi_uart_init(UART_BUS_0, &pin_config0, &attr, NULL, &uart_buffer_config);
    //int res1 = uapi_uart_init(UART_BUS_1, &pin_config1, &attr, NULL, &uart_buffer_config);
    int res2 = uapi_uart_init(UART_BUS_2, &pin_config2, &attr, NULL, &uart_buffer_config);
    if (res0 != 0) {
        printf("uart init failed res = %02x\r\n", res0);
    }
     /*if (res1 != 0) {
        printf("uart init failed res = %02x\r\n", res1);
    }*/
    if (res2 != 0) {
        printf("uart init failed res = %02x\r\n", res2);
    }
  /*  if (uapi_uart_register_rx_callback(CONFIG_UART_ID, UART_RX_CONDITION_MASK_IDLE, 1, sle_server_read_handler) ==
        ERRCODE_SUCC) {
        printf("uart%d int mode register receive callback succ!\r\n", CONFIG_UART_ID);
    }*/
  /* if (uapi_uart_register_rx_callback(UART_BUS_1, UART_RX_CONDITION_MASK_IDLE, 1, uart1_read_handler) ==
        ERRCODE_SUCC) {
        printf("uart%d int mode register receive callback succ!\r\n", UART_BUS_1);
    }*/
   if (uapi_uart_register_rx_callback(UART_BUS_2, UART_RX_CONDITION_MASK_IDLE, 1, uart2_read_handler) ==
        ERRCODE_SUCC) {
        printf("uart%d int mode register receive callback succ!\r\n", UART_BUS_2);
    }
}
